﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Xushre.WeiBoSDK {

    public partial class OClient2 {
        /// <summary>
        /// 根据用户ID获取用户信息
        /// </summary>
        /// <param name="uid">需要查询的用户ID。</param>
        /// <param name="screen_name">需要查询的用户昵称。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string UsersShow(Int64 uid = 0, string screen_name = null) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            if (uid > 0) {
                data.Add("uid", uid);
            } else if (!String.IsNullOrEmpty(screen_name)) {
                data.Add("screen_name", screen_name);
            } else {
                return "{\"error\":\"Parameters are screen_name and UID will choose one of the two, and only one of.\",\"error_code\":9003}";
            }

            try {
                response = Get(API.OClient2.Users.SHOW, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 通过个性化域名获取用户资料以及用户最新的一条微博
        /// </summary>
        /// <param name="domain">需要查询的个性化域名。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string UsersDomainShow(string domain) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("domain", domain);

            try {
                response = Get(API.OClient2.Users.DOMAIN_SHOW, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 批量获取用户的粉丝数、关注数、微博数
        /// </summary>
        /// <param name="uids">需要获取数据的用户UID，最多不超过100个。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string UsersCounts(Int64[] uids) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            if (uids.Length > 100) {
                return "{\"error\":\"Parameter must be less than the number of UIDs 100.\",\"error_code\":9014}";
            }

            data.Add("uids", String.Join(",", values: uids));

            try {
                response = Get(API.OClient2.Users.COUNTS, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 获取用户等级信息
        /// </summary>
        /// <param name="uid">需要查询的用户ID。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string UsersShowRank(int uid) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("uid", uid);

            try {
                response = Get(API.OClient2.Users.SHOW_RANK, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 获取用户主页置顶微博
        /// </summary>
        /// <param name="uid">需要查询的用户UID。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string UsersGetTopStatus(Int64 uid) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("uid", uid);

            try {
                response = Get(API.OClient2.Users.GET_TOP_STATUS, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 设置当前用户主页置顶微博
        /// </summary>
        /// <param name="id">需要设置为置顶微博的微博ID。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string UsersSetTopStatus(Int64 id) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("id", id);

            try {
                response = Post(API.OClient2.Users.SET_TOP_STATUS, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 取消当前用户主页置顶微博
        /// </summary>
        /// <param name="id">需要取消设置为置顶微博的微博ID。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string UsersCancelTopStatus(Int64 id) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("id", id);

            try {
                response = Post(API.OClient2.Users.CANCEL_TOP_STATUS, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }
    }
}
